<?php echo $header; ?>

<?php if(isset($error)) { ?> 
    <div class="warning"><?php echo $error; ?></div>
<?php } ?>
<?php if(isset($success)){ ?>
    <div class="success"><?php echo $success;?></div>
<?php } ?>
        
<div class="box">
    <div class="left"></div>
    <div class="right"></div>
    <div class="heading">
        <h1 style="background-image: url('view/image/newsletter.png');"><?php echo $form_title; ?></h1>
        <div class="buttons">
             <?php if(!$item_send){ ?>
                <a id="send_newsletter" href="#" class="button"><span><?php echo $text_send; ?></span></a>
                <a onclick="location = '<?php echo $cancel; ?>'" class="button"><span><?php echo $text_cancel; ?></span></a>
             <?php }else{ ?>
                <a onclick="location = '<?php echo $continue; ?>'" class="button"><span><?php echo $text_continue; ?></span></a>
             <?php } ?>
        </div>
    </div>
            
    <div class="content">   
        <?php if($item_send){ ?>
            <h1 style="text-align:center;">Template has been sent.</h1>
         <?php }else{ ?>   
            <form id="newsletter_form" action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
                <table class="form">                         
                    <tbody>
                        <?php if(!$item_send){ ?>
                            <tr>
                                <td><?php echo $text_from; ?></td>
                                <td>
                                    <?php echo $from_name . ' &lt;' . $from_email . '&gt;'; ?>
                                    <input type="hidden" name="from_email" value="<?php echo $from_email; ?>" />
                                    <input type="hidden" name="from_name" value="<?php echo 'Havhokeren Gilleleje'; //$from_name; ?>" />                                        
                                </td>
                            </tr>  
                            <tr>
                                <td><?php echo $text_to; ?><span class="required">*</span></td>
                                <td id="newsletter_list">
                                    <div class="scrollbox">
                                        <?php $class = 'odd'; $checked = ''; ?>
                                        <?php foreach ($subscribers as $item) { ?>
                                            <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                                            <?php if(!empty($to)) { ?>
                                                <?php $checked = in_array($item['email'], $to) ? 'checked="checked"' : ''; ?>
                                            <?php } ?>
                                            <div class="<?php echo $class; ?>">
                                                <input type="checkbox" name="to[]" value="<?php echo $item['email']; ?>" <?php echo $checked; ?> />
                                                <?php echo $item['email']; ?>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </td>
                            </tr>                                                        
                            <tr>
                                <td><?php echo $text_subject; ?><span class="required">*</span></td>
                                <td><input name="subject" value="<?php echo $subject; ?>" size="100" /></td>                                
                            </tr>
                            <tr>
                                <td><?php echo 'Template'; //$text_message; ?><span class="required">*</span></td>
                                <!-- td><textarea name="message" id="message"><?php echo $message; ?></textarea></td -->                                
                                <td>
                                    <select id="template" name="template">                                     
                                        <option value="0"<?php echo ($template == 0 ? ' selected="selected"' : '') ?>>--</option>
                                        <?php foreach($templates as $template_item){ ?>
                                            <option value="<?php echo $template_item['template_id'] ?>"<?php echo ($template == $template_item['template_id'] ? ' selected="selected"' : '') ?>><?php echo $template_item['code'] ?></option>
                                        <?php } ?>
                                    </select>
                                    &nbsp;&nbsp;&nbsp;&nbsp;
                                    <span><a id="preview_template" href="<?php echo $preview; ?>" target="_blank"> Click here to preview template</a></span>
                                </td>
                            </tr>  
                        <?php }else{ ?>
                            <tr>
                                <td align="center" colspan="2"><?php echo $text_sent; ?></td>
                            </tr>
							
                        <?php } ?>         
							<tr>
                                <td align="left"><?php echo 'File attach' ;?></td>
								<td align="left"><input name="uploadedfile" type="file" /></td>
                            </tr>			
                    </tbody>
                </table>   
            </form>               
         <?php } ?>     
								
    </div>
</div>
    
<?php echo $footer; ?>

<script type="text/javascript" src="view/javascript/ckeditor/ckeditor.js"></script>
<script type="text/javascript"><!--
    jQuery(document).ready(function(){
        /*
        var editor = CKEDITOR.replace('message', {
            filebrowserBrowseUrl: '<?php echo $image_path; ?>',
            filebrowserImageBrowseUrl: '<?php echo $image_path; ?>',
            filebrowserFlashBrowseUrl: '<?php $image_path; ?>',
            filebrowserUploadUrl: '<?php echo $image_path; ?>',
            filebrowserImageUploadUrl: '<?php $image_path; ?>',
            filebrowserFlashUploadUrl: '<?php $image_path; ?>',
        });         
         */
        
        jQuery('#preview_template').click(function(even){            
            if(jQuery('#template').val() == 0){
                even.preventDefault();
                
                alert('Please select a template!');
            }
            else{
                var url = jQuery(this).attr('href') + '&template_id=' + jQuery('#template').val();
                                
                jQuery(this).attr('href', url);
            }
        });
        
        jQuery('#send_newsletter').click(function(even){
            even.preventDefault(); 
            
            jQuery('#newsletter_form').submit();
        });
    });  
//-->
</script>